# -*- coding:utf-8 -*-
import pygame
from pygame.sprite import Sprite


#配置
class Settings:
    def __init__(self):
        self.screen_width=1000
        self.screen_height=500
        self.bg_color=(255,255,255)

        self.ship_spend_factor = 10

        self.bullet_spend_factor=12
        self.bullet_width=3
        self.bullet_height=12
        self.bullet_color = (202, 0, 24)

#飞船
class Ship:
    def __init__(self,screen,setting):
        self.screen=screen

        image=pygame.image.load('image/ship.png')
        self.image=pygame.transform.scale(image, (38, 50))
        self.rect=self.image.get_rect()
        self.screen_rect=screen.get_rect()

        self.rect.centerx=self.screen_rect.centerx
        self.rect.bottom=self.screen_rect.bottom
        self.moving_right=False
        self.moving_left=False
        self.moving_up=False
        self.moving_down=False
        self.v = setting.ship_spend_factor

    def blitme(self):
        self.screen.blit(self.image,self.rect)

    def update(self):
        if self.moving_right and self.rect.right<self.screen_rect.right:
            self.rect.centerx += self.v
        elif self.moving_left and self.rect.left>0:
            self.rect.centerx -= self.v
        elif self.moving_up and self.rect.top>0:
            self.rect.centery -= self.v
        elif self.moving_down and self.rect.bottom<self.screen_rect.bottom:
            self.rect.centery += self.v

#子弹
class Bullet(Sprite):
    def __init__(self,setting,screen,ship):
        super(Bullet,self).__init__()
        self.screen=screen

        self.rect=pygame.Rect(0,0,setting.bullet_width,setting.bullet_height)
        self.rect.centerx=ship.rect.centerx
        self.rect.top=ship.rect.top

        self.y=float(self.rect.y)

        self.color=setting.bullet_color
        self.spend_factor=setting.bullet_spend_factor

    def update(self):
        self.y-=self.spend_factor
        self.rect.y=self.y

    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)
